#include "metaclass.h"
#include "libloader.h"
#include <iostream>
//#include <QTextStream>
#include <stdio.h>


using namespace std;

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);


    /* Here is how we we get QTextStreams that look like iostreams */

//    QTextStream cout(stdout, QIODevice::WriteOnly);

    //    MetaClass mc;
    //    mc.setLibName("tlib");
    //    mc.setClassName("Calc");

    //    QObject* obj = mc.instantiate();

    //    MetaField mf;
    //    QString fn = "field1";
    //    mf.setFieldName(fn);

    //    mc.addField(&mf);
    QString p  = QCoreApplication::applicationDirPath() + "/lib/";
cout << p.toStdString();
    LibLoader* loader = LibLoader::getInstance();
    loader->loadLib("tlib");

    const MetaClass* clas = loader->getClass("Calc");
    if (clas) {
        cout << "metaClass found \n";

        printf("libPath " + clas->getLibPath().toAscii());
//        cout << "libPath " << clas->getLibPath().length();

        QObject *o = clas->instantiate();

        if (o) {
            cout << "instance is created \n";
        } else {
            cout << "instance NOT created \n";
        }


        o = loader->getClassInstance("Calc", "tlib");

        if (o) {
            cout << "instance is created \n";
        } else {
            cout << "instance NOT created \n";
        }
    } else {
        cout << "metaClass NOT found";
    }

//    if (obj) {
//        cout << "class instantiated";
//        //mf.set(obj, QVariant(15));
//        mc.getFields().value(fn)->set(obj, QVariant(25));
//        cout << "f = " << mf.get(obj).toInt();
//    } else {
//        cout << "class NOT instantiated";
//    }


//    char ch;
//    cin >> ch;

    return app.exec();
}
